<?php
require_once  '../classes/TestManager.php';
require_once  '../classes/TestDataManager.php';
require_once  '../classes/ConnectionDB.php';

$connection =ConnectionDB::getInstance()->connectToDB();

if(isset ($_POST["operation"]))
{
    $operation = $_POST["operation"];

    if($operation== "sendResults")
    {
        $codiceTest = $_POST["codiceTest"];
        $codiceStudente = $_POST["codiceStudente"];
        $numeroDomande = $_POST["numeroDomande"];
        $chiaveTest = $_POST["chiaveTest"];

        for($i=0;$i<$numeroDomande;$i++)
        {
            $codDomanda = $_POST["codiceDomanda".$i];
            $risp = $_POST["rispostaData". $i];
            TestDataManager::getInstance()->updateRisultatiStudenti($connection, $codiceTest, $chiaveTest, $codiceStudente, $codDomanda, $risp);
            TestDataManager::getInstance()->updateStatoStudenteTest($connection, $codiceTest, $chiaveTest, $codiceStudente, $chiaveTest);
        }
    }
    else if($operation=='sendResultsPdf')
    {
        $codiceTest = $_POST["codiceTest"];
        $codiceStudente = $_POST["codiceStudente"];
        $codDomanda = $_POST["codiceDomanda"];
        $risp = $_POST["rispostaData"];
        $chiaveTest = $_POST["chiaveTest"];
        TestDataManager::getInstance()->updateRisultatiStudenti($connection, $codiceTest, $chiaveTest, $codiceStudente, $codDomanda, $risp);
        TestDataManager::getInstance()->updateStatoStudenteTest($connection, $codiceTest, $chiaveTest, $codiceStudente, $chiaveTest);
    }
    else if($operation=='makePdf')
    {
        $codiceTest = $_POST["codiceTest"];
        $oreTest = $_POST["ore"];
        $minutiTest = $_POST["minuti"];
        $key = $_POST["key"];
        $classe = $_POST["classe"];
        TestManager::getInstance()->makePdf($connection, $codiceTest,  $oreTest, $minutiTest, $key, $classe );
    }
    else if($operation == 'makeTestPdf')
    {
        $codiceTest = $_POST["codiceTest"];
        $oreTest = $_POST["ore"];
        $minutiTest = $_POST["minuti"];
        $key = $_POST["key"];
        $classe = $_POST["classe"];
        TestManager::getInstance()->makeTestPdf($connection, $codiceTest, $oreTest, $minutiTest, $key, $classe );
    }
    else if($operation == "updateStatoStudenteTest")
    {

        $codiceTest = $_POST["codiceTest"];
        $codiceStudente = $_POST["codiceStudente"];
        $chiaveTest = $_POST["chiaveTest"];
echo "codice test " . $codiceTest . " studetne " . $codiceStudente . " chiave " . $chiaveTest;
        TestDataManager::getInstance()->updateStatoUtenteTest($connection, $codiceTest, $chiaveTest, $codiceStudente);

    }

    else if($operation == "sottomettiTest")
    {
        $codiceTest = $_POST["codiceTest"];
        $key = $_POST["key"];
        $classe = $_POST["classe"];
        TestManager::getInstance()->sottomettiTest($connection, $codiceTest, $key, $classe);
        $str="Location:../portal/newMenu.php";
        header($str);
    }
    else if($operation=="delete")
    {
        $codiceTest = $_POST["codiceTest"];
        $chiaveTest = $_POST["chiaveTest"];
        TestDataManager::getInstance()->deleteTestSottomesso($connection, $codiceTest, $chiaveTest);
    }
    else
    {
        $checks = $_POST["checks"];
        $nomeTest = $_POST["nomeTest"];
        $oreTest = $_POST["oreTest"];
        $minutiTest = $_POST["minutiTest"];
        $codiceTest = $_POST["codiceTest"];
        $filtered = $_POST["filtered"];
        TestManager::getInstance()->addQUestionToTest($connection, $checks, $codiceTest, $filtered);
        $str="Location:../report/testQuestions.php?codiceTest=".$codiceTest;
        header($str);
    }
}
else
{
    $checks = $_POST["checks"];
    $nomeTest = $_POST["nomeTest"];
    $oreTest = $_POST["oreTest"];
    $minutiTest = $_POST["minutiTest"];
    $codiceDisciplina = $_POST["codiceDisciplina"];
    TestManager::getInstance()->insertTest($connection, $checks, $nomeTest, $oreTest, $minutiTest, $codiceDisciplina);
    $str="Location:newTest_1.php";
    header($str);
}

?>
